package com.antler.smt.admin.controller;

import com.antler.smt.admin.annotation.OperateCut;
import com.antler.smt.admin.mapper.ProductionLogEntityMapper;
import com.antler.smt.entity.ProductionLogEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.response.admin.OrderManageListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 项目专员对生产日志进行审核
 *
 * @author zhangyi
 * @date 2018/5/30 16:04
 */
@RestController
@RequestMapping("/productLog")
@Api(description = "项目专员对生产日志进行审核")
public class ProductLogAuditController {
    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private ProductionLogEntityMapper productionLogEntityMapper;

    /**
     * 获取所有订单管理列表实体
     *
     * @return Result实体
     */
    @PostMapping("/auditLog")
    @ApiOperation(value = "审核日志状态（0：不通过（默认是不通过的） 1 ：通过）", httpMethod = "POST")
    public Result orderManageList(@RequestParam(value = "logId") Integer logId,
                                  @RequestParam(value = "status") Integer status) {

        DataValidator.isBlank(String.valueOf(logId),"日志id不能为空！");
        DataValidator.isBlank(String.valueOf(status),"状态不能修改！");
        Result result = null;
            ProductionLogEntity productionLogEntity = new ProductionLogEntity();
            productionLogEntity.setId(logId);
            //将状态位置1
            productionLogEntity.setLogStatus(status);
            int i = productionLogEntityMapper.updateByPrimaryKeySelective(productionLogEntity);
            result = (0 == i ? new Result(ResultCodeEnum.APPEND_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));
        return result;
    }



    /**
     * 删除生产日志
     *
     * @return Result实体
     */
    @PostMapping("/deleteLog")
    @ApiOperation(value = "删除生产日志", httpMethod = "POST")
    @OperateCut(content = "删除生产日志")
    public Result deleteLog(@RequestParam(value = "logId") Integer logId) {

        DataValidator.isBlank(String.valueOf(logId),"日志id不能为空！");

        Result result = null;
            int i = productionLogEntityMapper.deleteByPrimaryKey(logId);
            result = (0 == i ? new Result(ResultCodeEnum.APPEND_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));
        return result;
    }

    /**
     * 修改生产日志
     *
     * @return Result实体
     */
    @PostMapping("/updateLog")
    @ApiOperation(value = "修改生产日志(需要传入：日志id,订单id，内容)", httpMethod = "POST")
    @OperateCut(content = "修改生产日志")
    public Result updateLog(@RequestParam(value = "logId") Integer logId,
                            @RequestParam(value = "orderId") Long orderId,
                            @RequestParam(value = "content") String content) {

        DataValidator.isBlank(String.valueOf(logId),"日志id不能为空！");

        Result result = null;
            ProductionLogEntity productionLogEntity = new ProductionLogEntity();
            productionLogEntity.setId(logId);
            productionLogEntity.setLogStatus(1);
            productionLogEntity.setLogContent(content);
            productionLogEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
            productionLogEntity.setOrderId(orderId);
            int i = productionLogEntityMapper.updateByPrimaryKeySelective(productionLogEntity);
            result = (0 == i ? new Result(ResultCodeEnum.APPEND_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));
        return result;
    }

}
